﻿
@{
    ViewBag.Title = "调拨单详情";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@model IOA.MES.DataAccess.VModel.VAllocateOrder

@section Contents{
    <style>
        .layui-form-label {
            width: auto !important;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div>
        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            @{ string AllocateNo = Model == null ? string.Empty : Model.AllocateNo;}
                            <label class="layui-form-label">{{lang.Field.AllocateOrderNo}}： @Html.Raw(AllocateNo)</label>
                        </div>
                        <div class="layui-inline">
                            @{ string WorkOrderNo = Model == null ? string.Empty : Model.WorkOrderNo.ToString();}
                            <label class="layui-form-label">{{lang.Field.WorkorderNo}}： @Html.Raw(WorkOrderNo)</label>
                        </div>
                        <div class="layui-inline">
                            @{ string OrderNo = Model == null ? string.Empty : Model.OrderNo.ToString();}
                            <label class="layui-form-label">{{lang.Field.OrderNo}}： @Html.Raw(OrderNo)</label>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            @{ string FromWarehouseCode = Model == null ? string.Empty : Model.FromWarehouseCode;}
                            <label class="layui-form-label">{{lang.Field.FromWarehouseCode}}： @Html.Raw(FromWarehouseCode)</label>
                        </div>
                        <div class="layui-inline">
                            @{ string FromWarehouse = Model == null ? string.Empty : Model.FromWarehouse;}
                            <label class="layui-form-label">{{lang.Field.FromWarehouse}}： @Html.Raw(FromWarehouse)</label>
                        </div>
                        <div class="layui-inline">
                            @{ string ToWarehouseCode = Model == null ? string.Empty : Model.ToWarehouseCode;}
                            <label class="layui-form-label">{{lang.Field.ToWarehouseCode}}： @Html.Raw(ToWarehouseCode)</label>
                        </div>
                        <div class="layui-inline">
                            @{ string ToWarehouse = Model == null ? string.Empty : Model.ToWarehouse;}
                            <label class="layui-form-label">{{lang.Field.ToWarehouse}}： @Html.Raw(ToWarehouse)</label>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            @{ string AllocateTime = Model == null ? string.Empty : Model.AllocateTime.ToString();}
                            <label class="layui-form-label">{{lang.Field.AllocateTime}}： @Html.Raw(AllocateTime)</label>
                        </div>
                        <div class="layui-inline">
                            @{ string Remark = Model == null ? string.Empty : Model.Remark;}
                            <label class="layui-form-label">{{lang.Field.Remark}}： @Html.Raw(Remark)</label>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-card">
                <div class="layui-card-body">
                    <table id="tbAllocateProduct" lay-filter="tbAllocateProduct"></table>
                </div>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="tplToolbar">
    <button class="layui-btn" lay-event="addProduct"><i class="layui-icon layui-icon-add-circle"></i></button>
    <button class="layui-btn layui-btn-normal" lay-event="saveAllocateProducts"><i class="layui-icon layui-icon-ok-circle"></i></button>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        var products = [];

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form'], function () {
                var table = layui.table;
                var options = {
                    elem: '#tbAllocateProduct'
                    , url: '/Plan/QueryAllocateProductList'
                    , method: "post"
                    , defaultToolbar: []
                    , toolbar: '#tplToolbar'
                    , where: {
                        allocateNo: '@AllocateNo'
                    }
                    , page: false
                    , limit: 1000
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { type: 'numbers', title: '序号', width: 60, align: 'center', edit: 'text', fixed: "left" }
                        , { field: 'ProductCode', title: '物料编码（必填）', align: 'center', edit: 'text', minWidth: "150", fixed: "left" }
                        , { field: 'Quantity', title: '数量（必填）', align: 'center', edit: 'number', minWidth: "120" }
                        , { field: 'BatchNo', title: '批次号（必填）', align: 'center', edit: 'text', minWidth: "150" }
                        , { field: 'ProductName', title: '物料名称', align: 'center', edit: 'text', minWidth: "150" }
                        , { field: 'Specifications', title: '规格', align: 'center', edit: 'text', minWidth: "150" }
                        , { field: 'BatchNoBig', title: '托盘号', align: 'center', edit: 'text', minWidth: "150" }
                        , { field: 'BatchNoSmall', title: '箱号', align: 'center', edit: 'text', minWidth: "150" }
                        , { field: 'ProductionCompany', title: '生产厂家', align: 'center', edit: 'text', minWidth: "150" }
                        , {
                            title: '删除', align: 'center', width: 110, fixed: "right", templet: function (d) {
                                return '<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>';
                            }
                        }
                    ]]
                };
                table.render(options);
                table.on('toolbar(tbAllocateProduct)', function (obj) {
                    if (obj.event === 'addProduct') {
                        var oldData = table.cache.tbAllocateProduct;
                        oldData.unshift({
                            PkId: 0,
                            ProductCode: "",
                            ProductName: "",
                            BatchNo: "",
                            BatchNoBig: "",
                            BatchNoSmall: "",
                            ProductionCompany: "",
                            Specifications: "",
                            Quantity: ""
                        });
                        table.reload('tbAllocateProduct', {
                            url: '',
                            data: oldData
                        });
                    } else if (obj.event === "saveAllocateProducts") {
                        SaveAllocateProducts();
                    }
                });
                table.on('tool(tbAllocateProduct)', function (obj) {
                    console.log(obj);
                    if (obj.event === 'delete') {
                        var oldData = table.cache.tbAllocateProduct;
                        oldData.splice(obj.tr[0].rowIndex, 1);
                        table.reload('tbAllocateProduct', {
                            url: '',
                            data: oldData
                        });
                    }
                });

                function SaveAllocateProducts() {
                    var data = layui.table.cache.tbAllocateProduct;
                    var allocateNo = '@AllocateNo';

                    var canBeSaved = true;
                    var allocateProducts = [];
                    $.each(data, function (i, d) {
                        if (d.Quantity && isNaN(d.Quantity)) {
                            canBeSaved = false;
                            layer.msg('第' + (i + 1) + '行数量必须为整形数字', { icon: 5 });

                            return false;
                        }
                        if (d.BatchNo && !d.Quantity && d.Quantity != 0) {
                            canBeSaved = false;
                            layer.msg('请填写第' + (i + 1) + '行数量');

                            return false;
                        }
                        if (d.Quantity && !d.BatchNo) {
                            canBeSaved = false;
                            layer.msg('请填写第' + (i + 1) + '行批次号');

                            return false;
                        }

                        if (d.Quantity > 0) {
                            allocateProducts.push({
                                PkId: d.PkId,
                                AllocateNo: allocateNo,
                                ProductCode: d.ProductCode,
                                ProductName: d.ProductName,
                                BatchNo: d.BatchNo,
                                BatchNoBig: d.BatchNoBig,
                                BatchNoSmall: d.BatchNoSmall,
                                ProductionCompany: d.ProductionCompany,
                                Specifications: d.Specifications,
                                Quantity: d.Quantity
                            });
                        }
                    });
                    console.log(allocateProducts);

                    if (canBeSaved) {
                        var index = layer.load();
                        $.ajax({
                            url: '/Plan/SaveAllocateProducts',
                            data: {
                                allocateNo: '@AllocateNo',
                                products: allocateProducts
                            },
                            dataType: 'json',
                            success: function (res) {
                                layer.close(index);
                                if (res.ErrCode != 200) {
                                    return layer.msg(res.ErrMsg, { icon: 5 });
                                }

                                layer.msg('保存成功', { icon: 6 });
                            }
                        });
                    }
                }
            });
        }
    </script>
}
